package com.qianyu.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.TenderPurchaseDetail;
import com.qianyu.project.entity.TenderPurchasePlan;
import com.qianyu.project.mapper.TenderProjectInfoMapper;
import com.qianyu.project.mapper.TenderPurchasePlanMapper;
import com.qianyu.project.service.ITenderPurchaseDetailService;
import com.qianyu.project.service.ITenderPurchasePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 招标采购计划 服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-01-30
 */
@Service
public class TenderPurchasePlanServiceImpl extends ServiceImpl<TenderPurchasePlanMapper, TenderPurchasePlan> implements ITenderPurchasePlanService {

    @Autowired
    private TenderPurchasePlanMapper tenderPurchasePlanMapper;
    @Autowired
    private ITenderPurchaseDetailService tenderPurchaseDetailService;

    @Override
    public IPage<TenderPurchasePlan> getPageList(Map<String, String> params) {
        return tenderPurchasePlanMapper.selectPageList(PageUtil.createPage(params),params);
    }

    @Override
    public String getMaxCode() {
        String code = tenderPurchasePlanMapper.selectMaxCode();
        if(code == null){
            code = DateUtil.today().replaceAll("-", "") + "000";
        }
        return code;
    }

    @Transactional(rollbackFor = Exception.class)
    @FlowBegin("tender_purchase_plan")
    @Override
    public Integer saveWithDetails(@FlowEntry("id") TenderPurchasePlan tenderPurchasePlan, List<TenderPurchaseDetail> tenderPurchaseDetails) {
        //保存招标信息
        tenderPurchasePlan.setApproveStatus(1);
        tenderPurchasePlanMapper.insert(tenderPurchasePlan);
        //保存详情
        tenderPurchaseDetailService.saveBatch(tenderPurchaseDetails);
        //返回模块id做流程
        return 1;
    }

    @Override
    public TenderPurchasePlan getById(String id) {
        return tenderPurchasePlanMapper.selectById(id);
    }
}
